package com.kuang.JSON;

import com.google.gson.Gson;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by 郭浩 on 2020/9/24 21:24
 */
public class jsonTest {
    public static void main(String[] args) {
        //将java对象转换成json字符串格式传到前端
        User user = new User();
        user.setUsername("admin");
        user.setPassword("admin");

        User user2 = new User();
        user2.setUsername("admin2");
        user2.setPassword("admin2");

        String jsonStr = "{\"username\":\""+ user.getUsername() + "\",\"password\":\"" + user.getPassword() +"\"}";
        //校验json字符串是否正确
        //www.bejson.com
        System.out.println(jsonStr);

        List<User> userList = new ArrayList<>();
        userList.add(user);
        userList.add(user2);
        Gson gson = new Gson();
        String s = gson.toJson(userList);
        String s1 = gson.toJson(user);
        System.out.println(s);
        System.out.println(s1);
    }
}
